//
//  Controller01.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/2/27.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import Foundation
import UIKit


class Controller01: UIViewController {
    
    var countLabel : UILabel!;
    var count:Float = 0.0 {
        didSet {
            countLabel.text = count.formatString(format: ".1");
        }
    }
    
    var isPaused = true;
    var timer : Timer!;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.blue;
        let wappedView = UIView(frame: UIScreen.main.bounds);
        wappedView.backgroundColor = .white;
        self.configUI(with:wappedView);
    }
    
    func configUI(with view:UIView){
        let rect = CGRect.init(origin: CGPoint.init(x: 0, y: 70), size: CGSize.init(width: SCREEN_W, height: 100));
        
        do{
            countLabel = UILabel.init(frame:rect);
            countLabel.textColor =  UIColor.white;
            countLabel.textAlignment = NSTextAlignment.center;
            countLabel.font = UIFont.systemFont(ofSize: 50);
            countLabel.text = "0.0";
            view.addSubview(countLabel);
        }
        
        do{
            let startBtn = UIButton.init(frame: CGRect.init(x: 0, y: 300, width: 0.5 * SCREEN_W, height: SCREEN_H - 300));
            view.addSubview(startBtn);
            startBtn.setTitle("开始", for: .normal);
            startBtn.setBackgroundImage( UIImage.creatWithColor(UIColor.blue), for: .normal);
            startBtn.addTarget(self, action: #selector(startAction(btn:)), for:.touchUpInside);
        }
        
        do{
            let stopBtn = UIButton.init(frame: CGRect.init(x: 0.5 * SCREEN_W, y: 300, width: 0.5 * SCREEN_W, height: SCREEN_H - 300));
            view.addSubview(stopBtn);
            stopBtn.setTitle("暂停", for: .normal);
            stopBtn.setBackgroundImage( UIImage.creatWithColor(UIColor.green), for: .normal);
            stopBtn.addTarget(self, action: #selector(stopAction(btn:)), for: .touchUpInside);
        }
        
        do{
            let resetBtn = UIButton.init(frame: CGRect.init(x: SCREEN_W - 100, y: 100, width: 100, height: 30));
            view.addSubview(resetBtn);
            resetBtn.setTitleColor(UIColor.white, for: .normal);
            resetBtn.setTitle("reste", for: .normal);
            resetBtn.addTarget(self, action: #selector(resetAction), for: .touchUpInside);
        }
        
        
        
    }
    
    func resetAction() {
        if timer != nil && timer.isValid {
            timer.invalidate();
        }
        count = 0;
        isPaused = true;
    }
    
    func stopAction(btn : UIButton) {
        if !isPaused {
            timer.invalidate();
            isPaused = true;
        }
    }
    
    func startAction(btn : UIButton) {
        if isPaused {
            timer = Timer(timeInterval: 0.1, target: self, selector: #selector(timeRun), userInfo: self, repeats: true);
            RunLoop.main.add(timer, forMode: .commonModes);
            isPaused = false;
        }
    }
    
    func timeRun() {
        count += 0.1;
        print("\(count.formatString(format:".1"))");
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}













